import axios from 'axios';
import { showFailToast } from 'vant'

const instance = axios.create({
  baseURL: 'http://121.89.205.189:3000/api'
})

const list: string[] = ['/cart/list', '/cart/add', '/cart/remove', '/cart/updatenum']

// 添加请求拦截器
instance.interceptors.request.use( config => {
  if(list.indexOf(config.url || '') !== -1){
    const token:string = window.localStorage.getItem('token') || ''

    config.headers.token = token
  }
  // 在发送请求之前做些什么
  return config;
})

// 添加响应拦截器
instance.interceptors.response.use( response => {
  if (response.data.code === '10119'){
    showFailToast('登录已过期,请重新登录')
    return {}
  }


  // 对响应数据做点什么
  return response.data;
})

export default instance

